<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-18
 * Time: 22:59
 */

namespace app\admin\controller;


use app\base\BaseController;
use app\base\model\Artcomment;
use app\base\model\Article as ArticleModel;
use app\base\model\Sort as SortModel;
use think\Loader;
use think\Request;

class Article extends BaseController
{
    public function index()
    {
        $this->assign('title', '文章管理');
        return $this->fetch();
    }


    public function ajax()
    {

        $request = Request::instance();
        if (!$request->isAjax()) {
            return ['code'=>-1,'msg'=>'请求方式错误'];
        }
        $article = new ArticleModel();
        $limit = $request->param('limit');
        $offset = $limit * $request->param('page')-$limit;

        $total = $article->count('articid');
        $data = $article->limit($offset,$limit)->select();
        return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $data];
    }

    public function dell()
    {
        $request = Request::instance();
        $articid = $request->has('articid')?$request->param('articid'):false;
        if (!$articid) {
            return ['code' => -1, 'msg' => '文章id未找到'];
        }

        if (ArticleModel::destroy($articid)) {
            return ['code' => 1, 'msg' => '删除成功'];
        } else {
            return ['code' => -1, 'msg' => '删除失败'];
        }
    }


    public function edit()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $articid = $request->has('articid')?$request->param('articid'):false;
            if (!$articid) {
                return ['code' => -1, 'msg' => '文章id未找到'];
            }
            $data = $request->param();
            $validate = Loader::validate('PublishValidate');
            if (!$validate->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }
            $update= ArticleModel::where('articid', $data['articid'])->update([
                'sort' => $data['sort'],
                'title' => $data['title'],
                'content' => $data['content'],
            ]);

            if ($update) {
                return ['code'=>1,'msg'=>'修改成功'];
            }

            return ['code'=>-1,'msg'=>'修改失败'];
        }

        $article= ArticleModel::get($request->param('articid'));
        $this->assign('articid',$request->param('articid'));
        $this->assign('title',$article->title);
        $this->assign('sortlist', SortModel::all());
        $this->assign('sort',$article->getData('sort'));
        $this->assign('content',$article->content);
        return $this->fetch();
    }


    public function comment()
    {

        $request = Request::instance();
        if (!$request->isAjax()) {
            $this->assign('articid', $request->param('articid'));
            return $this->fetch();
        }
        $article = new Artcomment();
        $limit = $request->param('limit');
        $offset = $limit * $request->param('page')-$limit;

        $total = $article->count('articid');
        $data = $article->where('articid',$request->param('articid'))->limit($offset,$limit)->select();
        return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $data];
    }

    public function commentDell()
    {
        $request = Request::instance();
        $commentid = $request->has('commentid')?$request->param('commentid'):false;
        if (!$commentid) {
            return ['code' => -1, 'msg' => '评论id未找到'];
        }

        if (Artcomment::destroy($commentid)) {
            return ['code' => 1, 'msg' => '删除成功'];
        } else {
            return ['code' => -1, 'msg' => '删除失败'];
        }
    }






}